﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class TinhThanhDAL:StandardConnectDB,ITinhThanh
    {
        public List<DTO.TinhThanhDTO> GetlistTinhThanh()
        {
            List<TinhThanhDTO> listTinhThanh = new List<TinhThanhDTO>();
            DataTable dt = this.DTExecuteSelect("Select * from TinhThanh");
            foreach (DataRow dr in dt.Rows)
            {
                listTinhThanh.Add(ConvertObjectDataRowToTinhThanhDTO(dr));
            }
            return listTinhThanh;
            
        }

        public DTO.TinhThanhDTO GetTinhThanhByID(string MaTinhThanh)
        {
            TinhThanhDTO tinhthanh = new TinhThanhDTO();
            Dictionary<string,object> paramlist=new Dictionary<string,object>();
            paramlist.Add("@MaTinhThanh", MaTinhThanh);
            DataTable dt = this.DTExecuteSelect("SP_TinhThanh_GetByID", paramlist);
            tinhthanh = ConvertObjectDataRowToTinhThanhDTO(dt.Rows[0]);

            return tinhthanh;
            
        }

        public bool InsertTinhThanh(DTO.TinhThanhDTO tinhthanh)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaTinhThanh", tinhthanh.MaTinhThanh);
            paramlist.Add("@TenTinhThanh", tinhthanh.TenTinhThanh);
            paramlist.Add("@GhiChu", tinhthanh.GhiChu);
            paramlist.Add("@QuocGia", tinhthanh.QuocGia);
            return ExecuteUpdate("SP_TinhThanh_Insert", paramlist);
            
        }

        public bool DeleteTinhThanh(DTO.TinhThanhDTO tinhthanh)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@MaTinhThanh", tinhthanh.MaTinhThanh);

            return this.ExecuteUpdate("SP_TinhThanh_Delete", paramlist);
            
        }

        public bool UpdateTinhThanh(DTO.TinhThanhDTO tinhthanh)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaTinhThanh", tinhthanh.MaTinhThanh);
            paramlist.Add("@TenTinhThanh", tinhthanh.TenTinhThanh);
            paramlist.Add("@GhiChu", tinhthanh.GhiChu);
            paramlist.Add("@QuocGia", tinhthanh.QuocGia);
            return ExecuteUpdate("SP_TinhThanh_Update", paramlist);
            throw new NotImplementedException();
        }

        public DTO.TinhThanhDTO ConvertObjectDataRowToTinhThanhDTO(System.Data.DataRow dr)
        {
            if (dr != null)
            {
                TinhThanhDTO tinhthanh = new TinhThanhDTO();
                tinhthanh.MaTinhThanh = dr["MaTinhThanh"].ToString();
                tinhthanh.TenTinhThanh = dr["TenTinhThanh"].ToString();
                IQuocGia iquocgia = new QuocGiaDAL();
                if (dr["MaQuocGia"].ToString() != "")
                    tinhthanh.QuocGia = iquocgia.GetQuocGiaByID(dr["MaQuocGia"].ToString());
                
                tinhthanh.GhiChu = dr["GhiChu"].ToString();

                return tinhthanh;

            }
            else
                return null;
            
        }


        public List<DTO.TinhThanhDTO> GetAllTinhThanhByQuocGia(string QuocGia)
        {
            List<TinhThanhDTO> listTinhThanh = new List<TinhThanhDTO>();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaQuocGia", QuocGia);
            DataTable dt = DTExecuteSelect("SP_TinhThanh_GetByMaQuocGia", paramlist);
            foreach (DataRow dr in dt.Rows)
            {
                listTinhThanh.Add(ConvertObjectDataRowToTinhThanhDTO(dr));
            }
            return listTinhThanh;
            
            
        }
    }
}
